<?php
namespace app\admin\controller;

use app\admin\model\Role as RoleModel;
use app\admin\model\Permission as PermissionModel;
use app\BaseController;

class Role extends BaseController
{
    public function index()
    {
        return view();
    }
    public function list_data()
    {
        $roles = new RoleModel;
        $limit = input('limit');
        $roles = $roles->order('id','desc')->paginate($limit);
        return lay_data($roles);
    }

    public function save()
    {
        try{
            $data = $this->request->only(['name']);
            $check = RoleModel::where('name',$data['name'])->count();
            if($check){
                return msg_error('名称重复');
            }
            $user = (new RoleModel)->createAndLog($data);

            return return_true_or_false($user);
        }catch (\Exception $e){
            return msg_error($e->getMessage());
        }
    }


    public function update($id)
    {
        try {
            $field = $this->request->only(['name']);
            $role = RoleModel::find($id);
            if (!$role) {
                return msg_error('角色不存在');
            }

            $role->name = $field['name'];
            $info = $role->saveAndLog();

            return return_true_or_false($info);

        } catch (\Exception $e) {
            return msg_error($e->getMessage());
        }
    }
    public function delete($id)
    {
        try{
            $role = RoleModel::find($id);
            $info = $role->deleteAndLog();
            return return_true_or_false($info);
        }catch (\Exception $e){
            return msg_error($e->getMessage());
        }
    }
    public function role_permission($id): \think\response\Json
    {
        try{
            $role = RoleModel::find($id);
            $has = array_column($role->permissions->toArray(),'id');
            $permissions = PermissionModel::order('type','desc')->select();

            $group_permission = [];
            foreach ($permissions as $permission){
                if(in_array($permission->id,$has)){
                    $permission->has = true;
                }else{
                    $permission->has = false;
                }
                $group_permission[$permission->type][]=$permission;
            }
            return msg_success('ok', $group_permission);
        }catch (\Exception $e){
            return msg_error($e->getMessage());
        }
    }

    public function update_permission($role): \think\response\Json
    {
        try{
            $role = (new RoleModel)->with('permissions')->find($role);
            $check = $this->request->param('check');
            $id = $this->request->param('id');

            if(!power('role_permission_update','角色授权','role')){
                session('power','没有权限'.'(角色授权)');
                return return_true_or_false(false);
            }

            if($check == 'true'){
                $info = $role->permissions()->attach($id);
            }else{
                $info = $role->permissions()->detach($id);
            }

            return return_true_or_false($info);
        }catch (\Exception $e){
            return msg_error($e->getMessage());
        }
    }

}